<?php
if( ! defined('BASEPATH')) exit('No direct script access allowed');

class Worksheet_model extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
	}
	
	public function get_user($id)
	{
		$this->db->select('*');
		$this->db->from('users');
		$this->db->where('id',$id);
		return $this->db->get()->row_array();
	}

	public function save_task($insert)
	{
	 	$this->db->insert('worksheet',$insert);
	}
	
	public function get_worksheet($id=null,$year_id,$month_id)
	{
		//if(isset($id)){
		$where = array(
		'year_id'=> $year_id,
		'month_id' => $month_id,
		'user_id' => $id,		
		); 
		
		$this->db->order_by("month_id","desc");
		$this->db->order_by("date_id","desc");
		return $this->db->group_by('date_id')->get_where('worksheet',$where)->result();
	}
	
	
	
	public function get_worksheet_year()
	{
		$this->db->distinct('year_id');
		$this->db->select('year_id');
		$this->db->order_by("year_id","desc");		
		//$this->db->where('user_id', $this->session->userdata('user_id'));
		$query = $this->db->get('attendance_timings')->result();
		return $query;
	}
	
	public function get_worksheet_months($year_id)
	{
		$this->db->distinct('month_id');
		$this->db->select('month_id,year_id');
		$this->db->order_by("month_id","desc");
		$this->db->where('year_id', $year_id);
		//$this->db->where('user_id', $this->session->userdata('user_id'));
		return $query = $this->db->get('attendance_timings')->result();
	}
	
	public function get_worksheet_date_time_hashtag($id,$year_id,$month_id,$date,$match){		
		$this->db->select('*')->from('worksheet')->where(array('date_id'=>$date,'year_id'=>$year_id,'month_id'=>$month_id,'user_id'=>$id))->like('desc',$match);
		return $query = $this->db->get()->result();
	}
	

	public function get_worksheet_date_time($id,$year_id,$month_id,$date){		
		$this->db->select('*')->from('worksheet')->where(array('date_id'=>$date,'year_id'=>$year_id,'month_id'=>$month_id,'user_id'=>$id));
		return $query = $this->db->get()->result();
	}	
		
	public function delete_worksheet($id)
	{
		$this->db->delete('worksheet',array('id'=>$id));
	}
	
	public function get_total_working_hr($where,$date_id){
		return $this->db->get_where('daily_attendance_sheet',$where)->row()->$date_id;
	}
	
	public function get_attendance_timings($id,$year_id,$month_id,$date)
	{
		$this->db->select('*')->from('attendance_timings')->where(array('date_id'=>$date,'year_id'=>$year_id,'month_id'=>$month_id,'user_id'=>$id));
		return $query = $this->db->get()->result();
	}
	
	public function get_details($id){
		return $this->db->get_where('worksheet',array('id'=>$id))->row_array();
	}
	
	public function update($update,$id){
	$this->db->update('worksheet',$update,array('id'=>$id));
	}
	public function get_remarks($where){
		$this->db->select('*');
		$this->db->from('worksheet_remarks');
		$this->db->where($where);
		return $this->db->get()->result();
	}
	public function get_all_remarks($where){
		$this->db->select('*');
		$this->db->from('worksheet_remarks');
		$this->db->where($where);
		return $this->db->get()->result();
	}
	function get_worksheet_verify($where)
	{
		return $this->db->get_where('worksheet_verify',$where)->result();
	}
	
		
	function worksheet_verified($where)
	{
		foreach($where as $get)
		{
	$get_id = $this->db->get_where('worksheet_verify',array('user_id'=>$get['user_id'],'date_id'=>$get['date_id'],'month_id'=>$get['month_id'],'year_id'=>$get['year_id']))->result();
		}
		
		if(count($get_id)==0)
					{	
		foreach($where as $insert)
		{
					$this->db->insert('worksheet_verify',$insert);
		}}else{	
		foreach($where as $update)
		{	
			$this->db->delete('worksheet_verify',array('user_id'=>$update['user_id'],'date_id'=>$update['date_id'],'year_id'=>$update['year_id'],'month_id'=>$update['month_id'],'verify'=>$update['verify']));
			
		}}
		
	}
	
	public function get_all_worksheet_by_date($where){	
	
	 $query = $this->db->get_where('worksheet',$where)->result();
	 return ($query);
	$this->db->close();
	} 
	
	public function get_hash_fields()
	{
		$this->db->select('tag');
		$this->db->from('worksheet_hashtag');
		$get_result=$this->db->get()->result();
		return $get_result;
	}
	
	public function get_all_tagnames()
	{
		$this->db->select('*');
		$this->db->from('worksheet_hashtag');
		return $this->db->get()->result();
	}
	
	public function insert_hashtag($where)
	{
		
		$get_id = $this->db->get_where('worksheet_hashtag',array('tag'=>$where))->result();
	
		if(count($get_id)==0)
		{	
			$this->db->insert('worksheet_hashtag',array('tag'=>$where));
		}
		
	}
	
	
	public function get_worksheet_hashtag($id=null,$year_id,$match)
	{
		/*$where = array(
		'year_id'=> $year_id,
		
		'user_id' => $id,		
		);*/	
		$this->db->distinct('date_id');
		$this->db->select('worksheet.date_id,worksheet.month_id,worksheet.year_id,worksheet.user_id');
		$this->db->from('worksheet');
		$this->db->where('year_id',$year_id);
		$this->db->where('user_id',$id);
		$this->db->order_by("month_id","desc");
		$this->db->order_by("date_id","desc");
		$this->db->like('desc',$match);	
		//return $this->db->group_by('date_id')->get_where('worksheet',$where);
		//return $this->db->get_where('worksheet',$where);
		return $this->db->get()->result();
		
	}	
	
	public function search_tags($search,$user,$year_id)
	{
			$this->db->distinct('date_id');
		$this->db->select('worksheet.date_id,worksheet.month_id,worksheet.year_id,worksheet.user_id');
		//$this->db->select('*');
		$this->db->from('worksheet');
		$this->db->where('user_id',$user);
		//$this->db->where('month_id',$month_id);
		$this->db->where('year_id',$year_id);
		$this->db->order_by("month_id","desc");
		$this->db->order_by("date_id","desc");
		
		foreach($search as $s){
		if(!empty($s)){
		$this->db->like('desc',$s);	
		}
		}
		//$this->db->group_by('date_id');
		return $this->db->get()->result();
	}	
	
	
	public function search_worksheet_date_time($id,$year_id,$month_id,$date){		
		$this->db->select('*');
		$this->db->from('worksheet');
		$this->db->where(array('date_id'=>$date,'year_id'=>$year_id,'month_id'=>$month_id,'user_id'=>$id));
		
		$tagname = $this->input->post('tagname');	
		
		$t = array();
		if(!empty($tagname)){	
		foreach($tagname as $tagname){
					
			$t[] = $tagname;
			}
		}
		
		foreach($t as $s){
		if(!empty($s)){
		$this->db->like('desc',$s);	
		}
		}
		
		
		return $query = $this->db->get()->result();
	}	
	
	public function search_worksheet_date_time_more($id,$year_id,$month_id,$date,$hashtag){		
		$this->db->select('*');
		$this->db->from('worksheet');
		$this->db->where(array('date_id'=>$date,'year_id'=>$year_id,'month_id'=>$month_id,'user_id'=>$id));
		
		$tagname = $this->input->post('tagname');	
		
		$t = array();
		if(!empty($tagname)){	
		foreach($tagname as $tagname){
					
			$t[] = $tagname;
			}
		}
		
		if(empty($hashtag))
		{
		foreach($t as $s){
		if(!empty($s)){
		$this->db->like('desc',$s);	
		}
		}
		}else{
			$this->db->like('desc',$hashtag);	
		}
		
		
		return $query = $this->db->get()->result();
	}	
	
	
	public function add_work_color($id,$color){
		$this->db->update('worksheet',array('feed'=>$color),array('id'=>$id));
	}
	
	public function add_worksheet_remark($where)
	{			
	$this->db->insert('worksheet_remarks', $where);
	}
	
	public function delete_remarks($id)
	{
		$this->db->delete('worksheet_remarks',array('id'=>$id));
	}	
	function get_single_setting($name){
		$this->db->from('settings');
		$this->db->like('name',$name);
		$result = $this->db->get()->row()->value;
		return $result;
	}
	public function unfilled_worksheet($id,$year_id,$month_id)
		{			
			//$data = array();
			$this->db->select("*");
			$this->db->from('daily_attendance_sheet');
			$this->db->where('user_id',$id);
			$this->db->where('month_id',$month_id);
			$this->db->where('year_id',$year_id);
			$unfilled = $this->db->get()->result_array();
		//echo("Database</br>" . "ID->" . $id . "</br>Month->" . $month_id . "</br>Year->" . $year_id . "</br>");
		//print_r($unfilled);
		
		//exit();
		//print_r($presents);
		unset($unfilled[0]['id']);
		unset($unfilled[0]['year_id']);
		unset($unfilled[0]['month_id']);
		unset($unfilled[0]['user_id']);		
		
			/*getting the loop of present*/
			
			if($unfilled)
				{
					foreach($unfilled as $key=>$value)
					{
						foreach($value as $k=>$val)
						{						
							if(!($val=='absent') AND !($val=='sunday') AND !($val=='holiday') AND !($val=='paid_leave') AND !($val=='rejected') AND !($val=='unpaid_leave'))
								{
								//print_r($k);
								if(isset($k))
								$data[] = $k;
								//echo '<pre>';
								}
						}			
					}				
				}
			//print_r();
			//exit();
			if(isset($data))
			return $data;
			else
			return null;	
		}

}
?>
